#include"dWayMerge_class.h"
#include<cstdio>
#include<cstdlib>
#include<sstream>

using namespace std;

void dWayMerge::heapPrint()
{
	int i;
	std::cout << "printing the heap :";

	std::cout << std::endl;
	for(i=1;i<=(position);i++)
	{
		std::cout << (heapspace[i].data) <<'\t';
	}
	std::cout << std::endl << "done printing";
	std::cout << std::endl;
}


int dWayMerge::SWAP( Element* a, Element* b)
{
	Element tmp;
	tmp = *a;
	*a = *b;
	*b = tmp;
	return 0;
}

int dWayMerge::heapIntialize( int space )
{
	this->heapspace = new Element [space+1];
	if(heapspace == NULL)
		return 0;
	else
	{
		return 1;
	}
}

void dWayMerge::heapClean()
{
	delete this->heapspace;
}


int dWayMerge::heapIsEmpty()
{
	if(position != 0)
		return 0;
	else if (position == 0)
		return 1;
}

int dWayMerge::heapInsert(Element e)
{
	this->position = this->position+1;
	//std::cout << "value of position : " << position << std::endl ;
	heapspace[position].data = e.data;
	heapspace[position].index = e.index;
	fixHeapUp(position);
}

Element dWayMerge::heapRemoveMax()
{
	int local=position;
	SWAP(this->heapspace+1,this->heapspace+position);
	fixHeapDown(heapspace,1,position-1);
	this->position = this->position -1;
	return heapspace[local];
}


void dWayMerge::heapSortInterface()
{
heapSort(heapspace,1,position);
}

void dWayMerge::heapSort(Element * heapSpace , int left , int right )
{
	int i;
	int size = right - left + 1;
	for(i=size/2;i>=1;i--)
	{
		fixHeapDown(heapSpace+left-1,i, size);
	}
	while(size>1)
	{
		SWAP(heapSpace+left,heapSpace+size-1+left);
		fixHeapDown(heapSpace+left-1,1,--size);
	}
}

int dWayMerge::fixHeapUp(int position )
{
	while((position)>1 && (heapspace[(position)/2].data<heapspace[(position)].data))
	{
		SWAP(this->heapspace+(position)/2,this->heapspace+(position));
		(position) = (position)/2;
	}
}

int dWayMerge::fixHeapDown(Element * heapSpace , int pos , int position)
{
	int i;
	while(2*pos<=position)
	{
		i = 2*pos;
		if(i< position && (heapSpace[i].data<heapSpace[i+1].data))
			i++;
		if((heapSpace[pos].data>heapSpace[i].data))
			break;
		SWAP(heapSpace+pos,heapSpace+i);
		pos= i;
	}
}

/*

int main(int argc, char *argv[])
{
	if(argc!=3)
	{
		std::cout << "usage is : " << argv[0] << " [num] [size]" << std::endl;
		exit(0);
	}

	int num = atoi(argv[1]);
	int size = atoi(argv[2]);
	int count = 1;
	dWayMerge d;
	Element e;
	if(!d.heapIntialize(sizeof(int)*num))
	{
		std::cout << "failed to allocate memory";
	}
	outputelementstream os[num];
	outputelementstream dway;
	dway.create("text.txt");
        for(int i=0;i<num;i++)
        {
                stringstream ss;
                ss << i;
                os[i].create(ss.str().c_str());
                for(int j=0;j<size;j++)
                        os[i].write_s(size-j);
        }

        inputelementstream is[num];
	inputelementstream output;
        for(int i=0;i<num;i++)
        {
                stringstream ss;
                ss << i;
                is[i].open_s(ss.str().c_str());
		int a = is[i].read_next();
		if(a != EOF)
                {
                        e.data = a;
                        e.index = i;
                        d.heapInsert(e);
                }
                else
                    break;

        }
	/*
	for(int i=0;i<num;i++)
        {
                int a = is[i].read_next();
                if(a != EOF)
		{
			e.data = a;
			e.index = i;
			d.heapInsert(e);
		}
                else
                    break;
        }
	
	int counter = size*(num);
	while(counter)
	{
	e = d.heapRemoveMax();
	//cout << " max :: value: " << e.data << " index: " << e.index << std::endl;
       	dway.write_s(e.data);
	counter--;
	int a = is[e.index].read_next();
	if(a!=EOF)
	{
		e.data = a;
		d.heapInsert(e);
	}
	}


	std::cout << std::endl;
	counter = size*num;
	output.open_s("text.txt");
	while(counter)
	{
	cout << output.read_next();
	counter--;
	}
	
	
	d.heapPrint();
	for(int i=0;i<num;i++)
        {
                for(int j=0;j<size;j++)
                        std::cout << d.heapRemoveMax().data << '\t';
        }
	
	std::cout << std::endl;

}

*/

